package com.neusoft.admin.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import com.neusoft.admin.entity.TableData;
import com.neusoft.admin.method.GetColor;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class feedbackViewControl {
    @FXML
    private TableView<TableData> tableView;
    @FXML
    private Button backButton;
    private String feedbackId, feedbackerInfo, feedbackerContact, feedbackAddress, feedbackDescription, feedbackLevel,
            feedbackTime;

    @FXML
    public void initialize() {
        read();
        setTableView();
    }

    public feedbackViewControl() {
    }

    public feedbackViewControl(String feedbackId) {
        this.feedbackId = feedbackId;
        read();
    }

    public void backButtonAction() {
        Stage stage = (Stage) backButton.getScene().getWindow();
        stage.close();
    }

    public void read() {
        File missionFile = new File("src/com/neusoft/admin/data/mission.txt");
        File usersFile = new File("src/com/neusoft/admin/data/users.txt");
        try (BufferedReader br1 = new BufferedReader(new FileReader(missionFile));
                BufferedReader br2 = new BufferedReader(new FileReader(usersFile))) {
            if (!missionFile.exists()) {
                System.out.println("mission文件不存在");
                return;
            }
            if (!usersFile.exists()) {
                System.out.println("users文件不存在");
                return;
            }
            String line;
            if (feedbackId == null) {
                System.out.println("未查询到00");
                return;
            }
            while ((line = br1.readLine()) != null) {
                String[] parts = line.split(" ");
                if (parts[0].equals(feedbackId)) {
                    feedbackerInfo = parts[2];
                    feedbackerContact = parts[4];
                    feedbackAddress = parts[7] + parts[8] + parts[9];
                    feedbackLevel = parts[11];
                    feedbackDescription = parts[13];
                    feedbackTime = parts[15];
                    break;
                } else {
                    System.out.println("未查询到");
                }
            }
            while ((line = br2.readLine()) != null) {
                String[] parts = line.split(",");
                int nameIndex = parts[1].indexOf(" ");
                if (feedbackerInfo.equals(parts[1].substring(nameIndex + 1))) {
                    feedbackerInfo = "姓名:  " + feedbackerInfo +" "+ parts[0] +" "+ parts[2];
                    break;
                } else {
                    System.out.println("未查询到");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setTableView() {
        final double TABLE_HEIGHT = 523;
        final double TABLE_HEADER_HEIGHT = 30;
        final double TABLE_PADDING = 10;
        ObservableList<TableData> data = FXCollections.observableArrayList(
                new TableData("公众监督反馈信息编号", feedbackId),
                new TableData("反馈者信息", feedbackerInfo),
                new TableData("反馈者联系电话", feedbackerContact),
                new TableData("反馈信息所在地址", feedbackAddress),
                new TableData("反馈信息描述", feedbackDescription),
                new TableData("预估等级", feedbackLevel),
                new TableData("反馈日期时间", feedbackTime));
        tableView.setItems(data);
        double rowHeight = (TABLE_HEIGHT - TABLE_HEADER_HEIGHT - TABLE_PADDING) / data.size();
        tableView.setFixedCellSize(rowHeight);

        // 假设第二列是目标列，索引为 1
        if (tableView.getColumns().size() > 1) {
            TableColumn<TableData, String> targetColumn = (TableColumn<TableData, String>) tableView.getColumns()
                    .get(1);
            targetColumn.setCellFactory(col -> new TableCell<TableData, String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty || item == null) {
                        setText(null);
                        setStyle("");
                    } else {
                        setText(item);
                        setAlignment(Pos.CENTER);
                        // 判断是否为第六行（索引为 5）第二列
                        if (getIndex() == 5) {
                            // 设置第六行第二列的字体颜色，这里以红色为例
                            setStyle(GetColor.getColor(item) + "-fx-font-size: 18px;");
                        } else {
                            setStyle("");
                        }
                    }
                }
            });
        }
        for (TableColumn<TableData, ?> column : tableView.getColumns()) {
            column.setSortable(false);
        }
        tableView.setStyle("-fx-font-size: 18px;");
    }

}